package com.tienon.message.Test;

import java.io.IOException;
import java.io.OptionalDataException;
import java.io.StreamCorruptedException;
import java.util.List;

import javax.annotation.Resource;

import org.junit.Test;

import com.ibm.mq.MQException;
import com.tienon.message.entity.Push_App;
import com.tienon.message.mapper.Push_AppMapper;
import com.tienon.message.mq.ReceiveDataFromMQ;
import com.tienon.message.mq.SendDataToMQ;

public class TestSendDataToMQ extends UnitTestEnv {

	@Resource
	private Push_AppMapper appDao;

	@Resource
	private SendDataToMQ sendDataToMQ;
	
	@Resource
	private ReceiveDataFromMQ receiveDataFromMQ;
	
	@Test
	public void sendMsg() {
		List<Push_App> list = appDao.findTenDatas();
		System.out.println(list.size());
		for (Push_App app : list) {

			System.out.println(app.getSerial() + ":" + app.getMsg());

		}
		
		try {
			sendDataToMQ.openMQ();
			sendDataToMQ.sendMsgs(list);
			//SendDataToMQ.sendMsg(list.get(0));
			sendDataToMQ.closeMQ();
			
			
		} catch (MQException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
		
	}

	@Test
	public void receiveMsg(){
		try {
			
			receiveDataFromMQ.openMQ("AppMsg", "SendMsg");
			List<Object> list = receiveDataFromMQ.receiveAllMsgs();
			//List<Object> list = ReceiveDataFromMQ.receivePartMsgs(10);
			System.out.println(list.size());
			for(Object data : list){
				
				if(data instanceof Push_App){
					Push_App app = (Push_App) data;
					System.out.println("序列号:"+app.getSerial()+" 发送的消息是:" + app.getMsg()+" 发送消息的优先级:"+app.getPrior());
					
				}
				
			}
			receiveDataFromMQ.closeMQ();
			
			
			
		} catch (MQException e) {
			e.printStackTrace();
		} catch (StreamCorruptedException e) {
			e.printStackTrace();
		} catch (OptionalDataException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
